﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace HYRElectric.SystemDiagram
{
	public class DistributionBoxCommon
	{
		public static Image GetCellImageByName(string name, List<CellInfo> cells)
		{
			if (cells.Count == 0)
			{
				return null;
			}
			foreach (CellInfo cellInfo in cells)
			{
				if (cellInfo.name == name)
				{
					return cellInfo.image;
				}
			}
			return cells[cells.Count - 1].image;
		}

		public static string GetCellFamilyByName(string name, List<CellInfo> cells)
		{
			if (cells.Count == 0)
			{
				return null;
			}
			foreach (CellInfo cellInfo in cells)
			{
				if (cellInfo.name == name)
				{
					return cellInfo.family;
				}
			}
			return cells[cells.Count - 1].family;
		}

		public static int GetIndexByName(string name, List<CellInfo> cells)
		{
			if (cells.Count == 0)
			{
				return -1;
			}
			for (int i = 0; i < cells.Count; i++)
			{
				if (name == cells[i].name)
				{
					return i;
				}
			}
			return 0;
		}

		public static List<string> CalculateBusWireData(BusInfo busInfo, List<LoopInfo> loopInfo)
		{
			double num = DistributionBoxCommon.CalculateTotalLoad(loopInfo);
			double num2 = DistributionBoxCommon.CalculateTotalPc(loopInfo);
			double totalQc = DistributionBoxCommon.CalculateTotalQc(loopInfo);
			double num3 = DistributionBoxCommon.CalculateTotalIc(loopInfo, num2, totalQc);
			double num4 = DistributionBoxCommon.CalculateTotalCos(num2, totalQc);
			double num5 = num2 / num;
			string arg = Convert.ToInt32(DistributionBoxCommon.CalculateTotalSc(num2, totalQc)).ToString();
			string arg2 = num.ToString("0.00");
			string arg3 = num4.ToString("0.00");
			string arg4 = num5.ToString("0.00");
			string arg5 = num2.ToString("0.00");
			string arg6 = num3.ToString("0.00");
			return new List<string>
			{
				string.Format("{0}kW", arg),
				string.Format("Pn={0}kW", arg2),
				string.Format("CosØ={0}", arg3),
				string.Format("Kd={0}", arg4),
				string.Format("Pc={0}kW", arg5),
				string.Format("Ic={0}A", arg6),
				""
			};
		}

		public static double CalculateTotalLoad(List<LoopInfo> loopInfo)
		{
			double num = 0.0;
			for (int i = 0; i < loopInfo.Count; i++)
			{
				num += loopInfo[i].load;
			}
			return num;
		}

		public static double CalculateTotalPc(List<LoopInfo> loopInfo)
		{
			double num = 0.0;
			if (DistributionBoxCommon.CheckSinglePhase(loopInfo))
			{
				for (int i = 0; i < loopInfo.Count; i++)
				{
					num += loopInfo[i].Pc;
				}
			}
			else
			{
				double num2 = 0.0;
				double num3 = 0.0;
				double num4 = 0.0;
				double num5 = 0.0;
				double num6 = 0.0;
				for (int j = 0; j < loopInfo.Count; j++)
				{
					if (loopInfo[j].phaseSequence == "L1")
					{
						num2 += loopInfo[j].Pc;
					}
					else if (loopInfo[j].phaseSequence == "L2")
					{
						num3 += loopInfo[j].Pc;
					}
					else if (loopInfo[j].phaseSequence == "L3")
					{
						num4 += loopInfo[j].Pc;
					}
					else if (loopInfo[j].phaseSequence == "单相")
					{
						num5 += loopInfo[j].Pc;
					}
					else
					{
						num6 += loopInfo[j].Pc;
					}
				}
				double num7;
				if (num2 >= num3)
				{
					num7 = num2;
				}
				else
				{
					num7 = num3;
				}
				if (num7 <= num4)
				{
					num7 = num4;
				}
				num = (num7 + num5) * 3.0 + num6;
			}
			return num;
		}

		public static double CalculateTotalQc(List<LoopInfo> loopInfo)
		{
			double num = 0.0;
			if (DistributionBoxCommon.CheckSinglePhase(loopInfo))
			{
				for (int i = 0; i < loopInfo.Count; i++)
				{
					num += loopInfo[i].Qc;
				}
			}
			else
			{
				double num2 = 0.0;
				double num3 = 0.0;
				double num4 = 0.0;
				double num5 = 0.0;
				double num6 = 0.0;
				for (int j = 0; j < loopInfo.Count; j++)
				{
					if (loopInfo[j].phaseSequence == "L1")
					{
						num2 += loopInfo[j].Qc;
					}
					else if (loopInfo[j].phaseSequence == "L2")
					{
						num3 += loopInfo[j].Qc;
					}
					else if (loopInfo[j].phaseSequence == "L3")
					{
						num4 += loopInfo[j].Qc;
					}
					else
					{
						num6 += loopInfo[j].Qc;
					}
				}
				double num7;
				if (num2 >= num3)
				{
					num7 = num2;
				}
				else
				{
					num7 = num3;
				}
				if (num7 <= num4)
				{
					num7 = num4;
				}
				num = (num7 + num5) * 3.0 + num6;
			}
			return num;
		}

		public static double CalculateTotalIc(List<LoopInfo> loopInfo, double totalPc, double totalQc)
		{
			double num = Math.Sqrt(Math.Pow(totalPc, 2.0) + Math.Pow(totalQc, 2.0));
			double result;
			if (DistributionBoxCommon.CheckSinglePhase(loopInfo))
			{
				result = num * 1000.0 / 220.0;
			}
			else
			{
				result = num * 1000.0 / (Math.Sqrt(3.0) * 380.0);
			}
			return result;
		}

		public static double CalculateTotalSc(double totalPc, double totalQc)
		{
			return Math.Sqrt(Math.Pow(totalPc, 2.0) + Math.Pow(totalQc, 2.0));
		}

		public static double CalculateTotalCos(double totalPc, double totalQc)
		{
			return Math.Cos(Math.Atan(totalQc / totalPc));
		}

		public static double CalculatePc(double load, double coefficient)
		{
			return load * coefficient;
		}

		public static double CalculateQc(double Pc, double power)
		{
			return Pc * Math.Tan(Math.Acos(power));
		}

		public static bool CheckSinglePhase(List<LoopInfo> loopInfo)
		{
			bool flag = true;
			string phaseSequence = loopInfo[0].phaseSequence;
			for (int i = 1; i < loopInfo.Count; i++)
			{
				if (phaseSequence != loopInfo[i].phaseSequence)
				{
					flag = false;
					break;
				}
			}
			if (flag && "三相" == phaseSequence)
			{
				flag = false;
			}
			return flag;
		}

		public static double Average(List<Phase> phases, int num)
		{
			double num2 = 0.0;
			foreach (Phase phase in phases)
			{
				num2 += phase.value;
			}
			return num2 / (double)num;
		}
	}
}
